/**
 * 
 */
package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 湖南
 * 排名: 完成
 * 去年排名:完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-24
 */
public class Hunan extends Exam implements IRank
{
	public static void main(String[] args)
	{
		Student student = new Student("2524110195", "10132686", "422828199710040025", "15622826152803", "bl");
		Hunan gansu = new Hunan(student);
		System.out.println(gansu.login(""));
	}

	public Hunan(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://www.hneeb.cn/website/check_dl.asp?lb=3", mSha1);
		loginRequest.addHeader("Referer", "http://www.hneeb.cn/website/gk_cx_gk.asp");
		loginRequest.addParams("kshNo", mStudent.getStudentId());
		loginRequest.addParams("password", mStudent.getStudentPwd());
		loginRequest.addParams("Submit", "%B5%C7%C2%BC");

		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("考生号"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("请您先登陆网上咨询交流会"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("密码错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();
		Elements trsif = loginDocument.select("table[bgcolor=319ED6]").get(0).select("tr");
		result.put("exam_id", mStudent.getStudentId());
		result.put("name", trsif.get(1).select("td").get(1).text());
		result.put("ranking", getRank(45, trsif.get(3).select("td").get(0).text(), mStudent.getType()));
		result.put("province", "湖南省");

		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", trsif.get(3).select("td").get(2).text());
		scoreObject.put("数学", trsif.get(3).select("td").get(1).text());
		scoreObject.put("外语", trsif.get(3).select("td").get(3).text());
		scoreObject.put("总分", trsif.get(3).select("td").get(0).text());
		scoreObject.put("综合", trsif.get(5).select("td").get(0).text());
		scoreObject.put("优惠分", trsif.get(5).select("td").get(2).text());
		result.put("score", scoreObject.toString());
		return result.toString();
	}

	@Override
	public void initRank()
	{
		// 理 http://www.gxeduw.com/gaokao/2015124218.html
		// 文 http://www.gxeduw.com/gaokao/2015124219.html
		Request request = new Request("http://www.gxeduw.com/gaokao/2015124218.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 2; i < trs.size() - 1; i++)
		{
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text().replaceAll(" ", ""));
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(3).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(4).text());
			System.out.println(scores + ":" + counters + ":" + rankings);

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(45);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(LI_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
	}

	@Override
	public void lastYearRank()
	{
		// 理 http://www.gxeduw.com/gaokao/2014107225.html |
		// http://www.gxeduw.com/gaokao/2014107225_2.html |
		// http://www.gxeduw.com/gaokao/2014107225_3.html
		// 文 http://www.gxeduw.com/gaokao/2014107226.html |
		// http://www.gxeduw.com/gaokao/2014107226_2.html |
		// http://www.gxeduw.com/gaokao/2014107226_3.html
		Request request = new Request("http://www.gxeduw.com/gaokao/2014107225_3.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 1; i < trs.size(); i++)
		{
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text().replaceAll(" ", ""));
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(2).text());
			System.out.println(scores + ":" + counters + ":" + rankings);

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(14);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(LI_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
	}
}
